const path = require('path');
const fs = require('fs-extra')
const view_path = path.join(__dirname , "../dist")
class CopyPlugin {
    apply (compiler) {
        if(process.env.NODE_ENV == "production"){
            compiler.hooks.afterPlugins.tap('clean-dist', (compilation) => {
                fs.removeSync(path.join(__dirname ,  '../dist'))
            })
        }
        
        compiler.hooks.done.tap('CopyPlugin', (compilation) => {
            console.log('The compiler is starting a new compilation...')
            
            try {
                fs.copySync(path.join(__dirname , '../static/script/plugins'),path.join(__dirname ,  '../dist/static/script/plugins'))
                fs.copySync(path.join(__dirname , '../static/style/plugins'),path.join(__dirname ,  '../dist/static/style/plugins'))
                
                fs.copySync(path.join(__dirname , '../static/html'),path.join(__dirname ,  '../dist/static/html'))
                fs.copySync(path.join(__dirname , '../static/image'),path.join(__dirname ,  '../dist/static/image'))
                console.log('success!')
            } catch (err) {
                console.error(err)
            }
        })
    }
}

module.exports = CopyPlugin